//
//  NewFeatureController.m
//  CMJWeibo
//
//  Created by BH on 15/12/10.
//
//

#import "NewFeatureController.h"
#import "CMJTabbarController.h"
#import "UIView+MJ.h"
#import "CMJCommon.h"
#import "UIImage+MJ.h"
#import <Masonry/Masonry.h>

#define NewfeatureImageCount 4

@interface NewFeatureController ()<UIScrollViewDelegate>

@property(nonatomic,strong)UIPageControl *pageCtrl;

@end

@implementation NewFeatureController

-(void)viewDidLoad
{
    [super viewDidLoad];
    
    //添加scrollview
    [self addScrollview];
    
    //添加pagecontrol
    [self addPageCtrl];
}

/**
 *  添加UISrollView
 */
-(void)addScrollview
{
    // 添加UISrollView
    UIScrollView *scrollview = [[UIScrollView alloc] init];
    scrollview.frame = self.view.bounds;
    scrollview.delegate = self;
    [self.view addSubview:scrollview];
    
    // 添加图片
    CGFloat imageW = scrollview.width;
    CGFloat imageH = scrollview.height;
    for (int i=0; i<NewfeatureImageCount; i++) {
        // 创建UIImageView
        UIImageView *imageView = [[UIImageView alloc] init];
        NSString *name = [NSString stringWithFormat:@"new_feature_%d",i+1];
        if (FourInch) {
            //如果是4inch,主动去加载-568h结尾的图片
            name = [name stringByAppendingString:@"-568h"];
        }
        imageView.image = [UIImage imageWithName:name];
        [scrollview addSubview:imageView];
        
        // 设置frame
        imageView.x = imageW * i;
        imageView.width = imageW;
        imageView.height = imageH;
        imageView.y = 0;
        
        // 处理最后一个ImageView
        if (i == NewfeatureImageCount - 1) {
            [self setupLastImage:imageView];
        }
        
        // 设置其他属性
        scrollview.contentSize = CGSizeMake(imageW * NewfeatureImageCount, 0);
        scrollview.showsHorizontalScrollIndicator = NO;
        scrollview.pagingEnabled = YES;
        scrollview.backgroundColor = Color(246, 246, 246);
    }
}

/**
 *  处理最后一个imageView
 */
-(void)setupLastImage:(UIImageView *)imageView
{
    imageView.userInteractionEnabled = YES;
    
    //添加开始按钮
    [self addStartBtn:imageView];
    
    //添加分享按钮
    [self addShareBtn:imageView];
}

/**
 *  添加开始按钮
 */
-(void)addStartBtn:(UIImageView *)imageView
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [imageView addSubview:button];
    
    [button setBackgroundImage:[UIImage imageWithName:@"new_feature_finish_button"] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageWithName:@"new_feature_finish_button_highlighted"] forState:UIControlStateHighlighted];
    button.size = button.currentBackgroundImage.size;
    button.centerX = self.view.width * 0.5;
    button.centerY = self.view.height * 0.8;
    [button setTitle:@"开始体验" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(startClick) forControlEvents:UIControlEventTouchUpInside];
}

-(void)startClick
{
    // 显示状态栏
    UIApplication *application = [UIApplication sharedApplication];
    application.statusBarHidden = NO;
    
    // 切换window的rootViewController
    UIWindow *window = application.keyWindow;
    window.rootViewController = [[CMJTabbarController alloc] init];
}

/**
 *  添加分享按钮
 */
-(void)addShareBtn:(UIImageView *)imageView
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [imageView addSubview:button];
    
    [button setTitle:@"分享" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button.adjustsImageWhenHighlighted = NO;
    [button setImage:[UIImage imageWithName:@"new_feature_share_false"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageWithName:@"new_feature_share_false"] forState:UIControlStateSelected];
    [button addTarget:self action:@selector(shareClick:) forControlEvents:UIControlEventTouchUpInside];
    
    button.width = 200;
    button.height = 35;
    button.centerX = self.view.width * 0.5;
    button.centerY = self.view.height * 0.7;
}

-(void)shareClick:(UIButton *)button
{
    button.selected = !button.selected;
}

/**
 *  添加pageControl
 */
-(void)addPageCtrl
{
    UIPageControl *pageCtrl = [[UIPageControl alloc] init];
    pageCtrl.numberOfPages = NewfeatureImageCount;
    pageCtrl.centerX = self.view.width * 0.5;
    pageCtrl.centerY = self.view.height-30;
    pageCtrl.currentPageIndicatorTintColor = Color(253, 98, 42);
    pageCtrl.pageIndicatorTintColor = Color(189, 189, 189);
    [self.view addSubview:pageCtrl];
    
    self.pageCtrl = pageCtrl;
}

#pragma mark - scrollview delegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    double ratio = scrollView.contentOffset.x / scrollView.width;
    NSInteger pageNo = (NSInteger)(ratio+0.5);
    self.pageCtrl.currentPage = pageNo;
}

@end
